//
//  UIViewController+YCTools.m
//  Pods
//
//  Created by Apple on 2022/11/3.
//

#import "UIViewController+YCTools.h"
#import <objc/runtime.h>

@implementation UIViewController (YCTools)

- (void (^)(CGFloat))vcShowKeyBoardBlock {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setVcShowKeyBoardBlock:(void (^)(CGFloat))vcShowKeyBoardBlock {
    objc_setAssociatedObject(self, @selector(vcShowKeyBoardBlock), vcShowKeyBoardBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(void))vcHideKeyBoardBlock {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setVcHideKeyBoardBlock:(void (^)(void))vcHideKeyBoardBlock {
    objc_setAssociatedObject(self, @selector(vcHideKeyBoardBlock), vcHideKeyBoardBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)registKeyBoardNotification:(void (^)(CGFloat keyboardH))showKeyBoardBlock hideKeyBoardBlock:(void (^)(void))hideKeyBoardBlock {
    
    self.vcShowKeyBoardBlock = showKeyBoardBlock;
    self.vcHideKeyBoardBlock = hideKeyBoardBlock;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidAppearNotifications:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHidenNotifications:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardDidAppearNotifications:(NSNotification *)notifications {
    NSDictionary *userInfo = [notifications userInfo];
    CGFloat keyboardH = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    if (self.vcShowKeyBoardBlock) {
        self.vcShowKeyBoardBlock(keyboardH);
    }
}

- (void)keyboardDidHidenNotifications:(NSNotification *)notifications {
    if (self.vcHideKeyBoardBlock) {
        self.vcHideKeyBoardBlock();
    }
}

- (void)cancelKeyBoardNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

//+ (UIViewController *)getCurrentViewController {
//    UIWindow *window = [[UIApplication sharedApplication].delegate window];
//    UIViewController *topViewController = [window rootViewController];
//    while (true) {
//        if (topViewController.presentedViewController) {
//            topViewController = topViewController.presentedViewController;
//        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
//            topViewController = [(UINavigationController *)topViewController topViewController];
//        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
//            UITabBarController *tab = (UITabBarController *)topViewController;
//            topViewController = tab.selectedViewController;
//        } else {
//            break;
//        }
//    }
//    return topViewController;
//}

+ (UIViewController *)getCurrentViewController {
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *topViewController = [window rootViewController];
    while (true) {
        if (topViewController.presentedViewController && ![topViewController.presentedViewController isKindOfClass:NSClassFromString(@"HXCustomNavigationController")]) {
            topViewController = topViewController.presentedViewController;
        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
            topViewController = [(UINavigationController *)topViewController topViewController];
        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController *tab = (UITabBarController *)topViewController;
            topViewController = tab.selectedViewController;
        } else {
            break;
        }
    }
    return topViewController;
}


void __swipeback_swizzle(Class cls, SEL originalSelector) {
    NSString *originalName = NSStringFromSelector(originalSelector);
    NSString *alternativeName = [NSString stringWithFormat:@"swizzled_%@", originalName];

    SEL alternativeSelector = NSSelectorFromString(alternativeName);

    Method originalMethod = class_getInstanceMethod(cls, originalSelector);
    Method alternativeMethod = class_getInstanceMethod(cls, alternativeSelector);

    class_addMethod(cls,
                    originalSelector,
                    class_getMethodImplementation(cls, originalSelector),
                    method_getTypeEncoding(originalMethod));
    class_addMethod(cls,
                    alternativeSelector,
                    class_getMethodImplementation(cls, alternativeSelector),
                    method_getTypeEncoding(alternativeMethod));

    method_exchangeImplementations(class_getInstanceMethod(cls, originalSelector),
                                   class_getInstanceMethod(cls, alternativeSelector));
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __swipeback_swizzle(self, @selector(presentViewController:animated:completion:));
    });
}

- (void)swizzled_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    //设置满屏，不需要小卡片
    if(@available(iOS 13.0, *)) {
        if (viewControllerToPresent.modalPresentationStyle == UIModalPresentationAutomatic) {
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    [self swizzled_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end
